package sensorrcp;

import org.apache.derby.drda.NetworkServerControl;
import org.apache.derby.iapi.tools.run;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.udp.nml.UDPClient;
import com.udp.nml.UDPClientSpecifyingPort;
import com.udp.nml.UDPHandler;
import com.udp.nml.UDPPortSpecifyingServer;

public class DashboardView extends ViewPart 
{
	public static final String ID = "SensorRCP.dashboard";
	
	private Text port;
	
	private Button startButton;
	
	private Button stopButton;
	
	private UDPPortSpecifyingServer udpServer;
	
	private Button sendSampleRequestButton;
	
	private Button startDBButton;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) 
	{
		parent.setLayout(new RowLayout());
		
		port = new Text(parent, SWT.NORMAL);
		
		port.addModifyListener(new ModifyListener() 
		{
			@Override
			public void modifyText(ModifyEvent event) 
			{
				boolean isOkToEnable = true;
				
				try
				{
					int portValue = Integer.parseInt(port.getText());
				}
				catch (NumberFormatException nfe)
				{
					isOkToEnable = false;
				}
				
				startButton.setEnabled(isOkToEnable);
			}
		});
		
		startButton = new Button(parent, SWT.PUSH);
		startButton.setText("Start");
		startButton.addSelectionListener(
			new SelectionListener(){
				@Override
				public void widgetSelected(SelectionEvent arg0) 
				{
					int portValue = Integer.parseInt(port.getText());
					udpServer = new UDPPortSpecifyingServer(portValue, new UDPHandler());
					udpServer.setup();
					port.setEnabled(false);
					stopButton.setEnabled(true);
					startButton.setEnabled(false);
					sendSampleRequestButton.setEnabled(true);
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) 
				{

				}
			});
		
		stopButton = new Button(parent, SWT.PUSH);
		stopButton.setText("Stop");
		stopButton.setEnabled(false);
		stopButton.addSelectionListener
		(
				new SelectionListener() {
				
				@Override
				public void widgetSelected(SelectionEvent arg0) 
				{
					if (udpServer != null)
					{
						udpServer.stop();
						port.setEnabled(true);
						stopButton.setEnabled(false);
						startButton.setEnabled(true);
						sendSampleRequestButton.setEnabled(false);
					}
				}
				
				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) 
				{

					
				}
			});
		sendSampleRequestButton = new Button(parent, SWT.PUSH);
		sendSampleRequestButton.setText("Send UDP Request");
		sendSampleRequestButton.setEnabled(false);
		sendSampleRequestButton.addSelectionListener(
			new SelectionListener(){

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {

				}
	
				@Override
				public void widgetSelected(SelectionEvent arg0) 
				{
					int portValue = Integer.parseInt(port.getText());
					UDPClientSpecifyingPort udpClientSpecifyingPort = new UDPClientSpecifyingPort(portValue);
					udpClientSpecifyingPort.connect();
				}});
		
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() 
	{
		port.setFocus();
	}
}